import { Routes, Route, useNavigate, useLocation } from "react-router-dom";
import Home from "@/pages/Home";
import RegistrationPage from "@/pages/RegistrationPage";
import EntryPage from "@/pages/EntryPage";
import LoginPage from "@/pages/LoginPage";
import GeneratePage from "@/pages/GeneratePage";
import ReadingPage from "@/pages/ReadingPage";
import CollectionPage from "@/pages/CollectionPage";
import ProfilePage from "@/pages/ProfilePage";
import AgentPage from "@/pages/AgentPage";
import { useState } from "react";
import { AuthContext } from '@/contexts/authContext';
import { useTheme } from '@/hooks/useTheme';
import { createContext } from 'react';

// 创建主题上下文 - 提供更明确的类型定义
export const ThemeContext = createContext<{
  theme: Theme;
  isDark: boolean;
  toggleDarkMode: () => void;
}>({
  theme: 'gradient',
  isDark: false,
  toggleDarkMode: () => {}
});

// 导入主题类型
type Theme = 'gradient';

export default function App() {
  const [isAuthenticated, setIsAuthenticated] = useState(false);
  const [currentUser, setCurrentUser] = useState<string | null>(null);
  const { theme, isDark, toggleDarkMode } = useTheme();
  const [showAgentFloat, setShowAgentFloat] = useState(true);
  const navigate = useNavigate();
  const location = useLocation();

  // 登录处理
  const login = (username: string) => {
    setIsAuthenticated(true);
    setCurrentUser(username);
  };

  // 登出处理
  const logout = () => {
    setIsAuthenticated(false);
    setCurrentUser(null);
  };

  // 切换智能体悬浮按钮显示
  const toggleAgentFloat = () => {
    setShowAgentFloat(!showAgentFloat);
  };

  // 打开智能体页面
  const openAgentPage = () => {
    navigate('/agent');
  };

  return (
    <div>
      <AuthContext.Provider
        value={{ isAuthenticated, currentUser, login, logout }}
      >
        <ThemeContext.Provider value={{ theme, isDark, toggleDarkMode }}>
            <Routes>
              <Route path="/" element={<EntryPage />} />
              <Route path="/register" element={<RegistrationPage />} />
              <Route path="/login" element={<LoginPage />} />
              <Route path="/home" element={<Home />} />
              <Route path="/generate" element={<GeneratePage />} />
              <Route path="/read/:storyId" element={<ReadingPage />} />
              <Route path="/collection" element={<CollectionPage />} />
              <Route path="/profile" element={<ProfilePage />} />
              <Route path="/agent" element={<AgentPage />} />
              {/* 为/runtime/路径添加重定向 */}
              <Route path="/runtime/" element={<EntryPage />} />
            </Routes>
        </ThemeContext.Provider>
      </AuthContext.Provider>

      {/* 全局悬浮智能体按钮 */}
{showAgentFloat && isAuthenticated && location.pathname !== '/agent' && (
        <button
          id="agent-float-button"
          onClick={openAgentPage}
          className="fixed bottom-24 right-4 w-16 h-16 bg-transparent rounded-full shadow-lg flex items-center justify-center z-50 hover:scale-110 transition-transform duration-300"
          aria-label="打开童话家"
          title="和童话家聊天"
        >
                <img 
                  src="https://lf-code-agent.coze.cn/obj/x-ai-cn/307173148162/attachment/e7cb1a9e560c13e8924d04f1d807bc45_20251022234653.jpg" 
                  alt="童话家智能助手" 
                  className="w-16 h-16 rounded-full object-cover" 
                />
        </button>
      )}
    </div>
  );
}